/*
 *  SBJ/NXT/DeviceBrowser.cpp
 *  NXTFace
 *
 *  Created by David Giovannini on 8/23/06.
 *  Copyright 2006 Software by Jove. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "SBJ/NXT/DeviceBrowser.h"

#include "fantom/iNXT.h"
#include "fantom/iNXTIterator.h"
#include "fantom/tStatus.h"

namespace SBJ
{

namespace NXT
{

struct DeviceBrowser::Entry
{
	Entry( const std::string& rn, nFANTOM100::iNXT* nxt = 0 )
	: index( -1 ), device( rn, nxt )
	{
	}
	
	size_t index;
	Device device;
};

DeviceBrowser::DeviceBrowser( int bluetoothTimeOutSecs )
: m_bluetoothTimeOut( bluetoothTimeOutSecs )
{
	scan();
}

DeviceBrowser::~DeviceBrowser( void )
{
	m_nxtSeq.clear();
	NxtMap::reverse_iterator iter( m_nxtMap.rbegin() );
	while ( iter != m_nxtMap.rend() )
	{
		delete iter->second;
		++iter;
	}
}

void
DeviceBrowser::scan( void )
{
	{
		nFANTOM100::tStatus status;
		// scan non-bluetooth
		nFANTOM100::iNXTIterator* iter = nFANTOM100::iNXT::createNXTIterator( false, 0, status );
		while ( status.isNotFatal() )
		{
			ViChar deviceSessionName[ 256 ];
			::memset( deviceSessionName, 0, sizeof( deviceSessionName ) );
			iter->getName( deviceSessionName, status );
			deviceSessionName[ 24 ] = 0;
			std::pair< NxtMap::iterator, bool > insertIt( m_nxtMap.insert( std::make_pair( deviceSessionName, (Entry*)0 ) ) );
			Entry*& entry( insertIt.first->second );
			if ( insertIt.second == true )
			{
				entry = new Entry( deviceSessionName, iter->getNXT( status ) );
			}
			iter->advance( status );
		}
		nFANTOM100::iNXT::destroyNXTIterator( iter );
	}
	{
		nFANTOM100::tStatus status;
		// scan bluetooth
		nFANTOM100::iNXTIterator* iter = nFANTOM100::iNXT::createNXTIterator( true, m_bluetoothTimeOut, status );
		while ( status.isNotFatal() )
		{
			ViChar deviceSessionName[ 256 ];
			::memset( deviceSessionName, 0, sizeof( deviceSessionName ) );
			iter->getName( deviceSessionName, status );
			deviceSessionName[ 24 ] = 0;
			std::pair< NxtMap::iterator, bool > insertIt( m_nxtMap.insert( std::make_pair( deviceSessionName, (Entry*)0 ) ) );
			Entry*& entry( insertIt.first->second );
			if ( insertIt.second == true )
			{
				entry = new Entry( deviceSessionName, iter->getNXT( status ) );
			}
			iter->advance( status );
		}
		nFANTOM100::iNXT::destroyNXTIterator( iter );
	}
	{
		// Recreate the sequence
		int i = 0;
		m_nxtSeq.clear();
		NxtMap::iterator iter( m_nxtMap.begin() );
		while ( iter != m_nxtMap.end() )
		{
			m_nxtSeq.push_back( iter->second );
			iter->second->index = i;
			++i;
			++iter;
		}
	}
}

size_t
DeviceBrowser::getCount( void ) const
{
	return m_nxtMap.size();
}

Device*
DeviceBrowser::getDevice( const std::string& sessionName, const std::string& blueToothPassKey )
{
	NxtMap::iterator findIt = m_nxtMap.find( sessionName );
	if ( findIt != m_nxtMap.end() )
	{
		Entry* entry = findIt->second;
		bool good = entry->device.connect( blueToothPassKey );
		if ( ! good )
		{
		//	m_nxtSeq.erase( m_nxtSeq.begin() + findIt->second->index );
		//	m_nxtMap.erase( findIt );
		//	delete entry;
		//	return 0;
		}
		return &entry->device;
	}
	return 0;
}

Device*
DeviceBrowser::getDevice( size_t index, const std::string& blueToothPassKey )
{
	if ( index < m_nxtSeq.size() )
	{
		Entry* entry = m_nxtSeq[ index ];
		bool good = entry->device.connect( blueToothPassKey );
		if ( ! good )
		{
		//	m_nxtSeq.erase( m_nxtSeq.begin() + index );
		//	m_nxtMap.erase( m_nxtMap.find( entry->device.getSessionName() ) );
		//	delete entry;
		//	return 0;
		}
		return &entry->device;
	}
	return 0;
}

}

}